package org.dromara.customer.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.customer.domain.bo.TaOrderDetailBo;
import org.dromara.customer.domain.vo.TaOrderDetailVo;
import org.dromara.customer.domain.TaOrderDetail;
import org.dromara.customer.mapper.TaOrderDetailMapper;
import org.dromara.customer.service.ITaOrderDetailService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 订单明细Service业务层处理
 *
 * @author xiyin321
 * @date 2024-07-09
 */
@RequiredArgsConstructor
@Service
public class TaOrderDetailServiceImpl implements ITaOrderDetailService {

    private final TaOrderDetailMapper baseMapper;

    /**
     * 查询订单明细
     */
    @Override
    public TaOrderDetailVo queryById(Long orderDetailId){
        return baseMapper.selectVoById(orderDetailId);
    }

    /**
     * 查询订单明细列表
     */
    @Override
    public TableDataInfo<TaOrderDetailVo> queryPageList(TaOrderDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TaOrderDetail> lqw = buildQueryWrapper(bo);
        Page<TaOrderDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询订单明细列表
     */
    @Override
    public List<TaOrderDetailVo> queryList(TaOrderDetailBo bo) {
        LambdaQueryWrapper<TaOrderDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TaOrderDetail> buildQueryWrapper(TaOrderDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TaOrderDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getOrdersId() != null, TaOrderDetail::getOrdersId, bo.getOrdersId());
        lqw.eq(bo.getImage() != null, TaOrderDetail::getImage, bo.getImage());
        lqw.eq(bo.getGoodsId() != null, TaOrderDetail::getGoodsId, bo.getGoodsId());
        lqw.like(StringUtils.isNotBlank(bo.getGoodsName()), TaOrderDetail::getGoodsName, bo.getGoodsName());
        lqw.eq(StringUtils.isNotBlank(bo.getSelected()), TaOrderDetail::getSelected, bo.getSelected());
        lqw.eq(StringUtils.isNotBlank(bo.getAmount()), TaOrderDetail::getAmount, bo.getAmount());
        return lqw;
    }

    /**
     * 新增订单明细
     */
    @Override
    public Boolean insertByBo(TaOrderDetailBo bo) {
        TaOrderDetail add = MapstructUtils.convert(bo, TaOrderDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改订单明细
     */
    @Override
    public Boolean updateByBo(TaOrderDetailBo bo) {
        TaOrderDetail update = MapstructUtils.convert(bo, TaOrderDetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TaOrderDetail entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除订单明细
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
